<template>
	<view class="mall">
		<view>
			<image src="http://pic.yupoo.com/hisbody/ff93f1a1/6ad851ac.jpg" class="headerBg"></image>
		</view>
		<view class="navBg">
			<scroll-view scroll-x>
				<uni-segmented-control class="segmented" :current="current" :values="items" @clickItem="onClickItem" style-type="text"
				 active-color="#FD4442" color="#fff"></uni-segmented-control>
			</scroll-view>
		</view>
		<scroll-view scroll-y="true" class="scroll-view" show-scrollbar refresher-enabled @refresherrefresh="refresherrefresh"
		 :refresher-triggered="triggered" @scroll="scrollHandle">
			<view class="content">
				<view>
					<!-- 商品列表 -->
					<view class="goods-list">
						<!-- <navigator url="/pages/detail/detail" > -->
						<view @click="toDetail(item)" class="goods-item" v-for="(item,index) in goodsList" :key="index">
							<view class="goods-img">
								<image class="grid-middle" :src="item.img"></image>
							</view>
							<view class="goods-bottom-wrap">
								<view class="goods-description">
									{{item.good_name}}
								</view>
								<view class="price-wrap">
									<view class="price">
										¥{{item.price}}
									</view>
									<view class="icon">
										<!-- <uni-icons type="cart-filled" size="20" color="#FF4B4B"></uni-icons> -->
									</view>
								</view>
							</view>
						</view>
						<!-- </navigator> -->
					</view>
				</view>
			</view>
			<view class="bottom-box">
				<image src="/static/home/bottom_log.png">
			</view>
		</scroll-view>
		<view :class="['suspensionBtn',{'hiddenBtn':isHiden}]">
			<navigator url="/pages/shopcat/shopcat" hover-class="none">
				<uni-icons type="cart" size="24" color="#9B9B9B"></uni-icons>
			</navigator>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				datas: [],
				items: [],
				current: 0,
				goodsList: [],
				triggered: false,
				isHiden: false,
				timeout: ''
			};
		},

		methods: {
			//初始化
			async onShowTab() {
				let res = await this.$http('api/goodType')
				this.datas = res.data.data;
				for (let i = 0; i < this.datas.length; i++) {
					this.items.push(this.datas[i].type_name)
				}

				res = await this.$http('api/goodList', {
					search: this.items[this.current]
				})
				this.goodsList = res.data.data.data
			},
			// 分段器切换
			onClickItem(e) {
				this.current = e.currentIndex
				this.requestGoodsList();
			},

			// 获取商品列表
			async requestGoodsList() {
				let res = await this.$http('api/goodList', {
					search: this.items[this.current]
				});
				this.goodsList = res.data.data.data
			},
			// 下拉刷新
			refresherrefresh() {
				this.triggered = true;
				this.curryingFunc(this.current);
			},
			// 页面滚动事件
			scrollHandle(e) {
				clearTimeout(this.timeout);
				this.timeout = setTimeout(() => {
					e.detail.scrollTop > 200 ? (this.isHiden = true) : (this.isHiden = false);
				}, 300)
			},
			//详情跳转
			toDetail(item){
				uni.navigateTo({
					url:'/pages/detail/detail?good_id=' + item.good_id+'&search=' + this.items[this.current]
				})
			}
		},
		created() {
			this.onShowTab()
		}
	};
</script>
<style lang="scss" scoped>
	.headerBg {
		width: 100%;
		height: 370rpx;
	}

	.navBg {
		border-bottom-left-radius: 5px;
		border-bottom-right-radius: 5px;
		background: #000 url("//pic.yupoo.com/hisbody/f3635252/c352ce42.png") no-repeat center center;
	}

	.scroll-view {
		width: 100%;
		height: calc(100vh - 36px);
	}

	.hiddenBtn {
		right: -120rpx !important;
	}

	.suspensionBtn {
		position: fixed;
		bottom: 40rpx;
		right: 20rpx;
		width: 90rpx;
		height: 90rpx;
		background-color: #E5E5E5;
		border-radius: 45rpx;
		transition: right .2s linear;
		overflow: hidden;

		navigator {
			width: 100%;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}

	.bottom-box {
		height: 160rpx;
		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;

		image {
			width: 40rpx;
			height: 79rpx;
		}
	}

	.mall {
		min-height: 100vh;
		background-image: url(http://pic.yupoo.com/isfy666/9e17ec4f/6b309d5f.jpg);
		background-size: cover;
		background-repeat: no-repeat;
	}

	.goods-list {
		width: 710rpx;
		margin: 0 20rpx;
		overflow: hidden;

		&>.goods-item:nth-child(2n-1) {
			margin-right: 20rpx;
		}

		.goods-item {
			float: left;
			width: 345rpx;
			margin-top: 20rpx;
			height: 470rpx;
			border-radius: 10rpx;
			background: #fff;
			overflow: hidden;
			display: flex;
			flex-flow: column nowrap;

			.goods-img {
				width: 100%;
				height: 342rpx;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.goods-bottom-wrap {
				height: 128rpx;
				padding: 15rpx;

				.goods-description {
					height: 66rpx;
					font-size: 12px;
					overflow: hidden;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
				}

				.price-wrap {
					height: 32rpx;
					display: flex;
					flex-flow: row nowrap;
					justify-content: space-between;
					align-items: center;
					font-size: 14px;
					color: #FF4B4B;
				}

			}
		}
	}
</style>
